package common

type BizCode int

const SuccessCode BizCode = 200

type Result struct {
	Code    BizCode     `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func (r *Result) Fail(code BizCode, msg string) {
	r.Code = code
	r.Message = msg
}
func (r *Result) Success(data interface{}) {
	r.Code = SuccessCode
	r.Message = "Success"
	r.Data = data
}
func (r *Result) Deal(data interface{}, err error) *Result {
	if err != nil {
		r.Fail(-999, err.Error())
	}
	r.Success(data)
	return r
}
func (r *Result) NoLogin(code BizCode, msg string) *Result {
	r.Code = code
	r.Message = msg
	return r
}
func NewResult() *Result {
	return &Result{}
}

// ----------
type Results struct {
	Code    BizCode       `json:"code"`
	Message string        `json:"message"`
	Data    []interface{} `json:"data"`
}

func (r *Results) Success(data interface{}) {
	r.Code = SuccessCode
	r.Message = "Success"
	r.Data = append(r.Data, data)
}
func (r *Results) Deal(data interface{}, err error) *Results {
	if err != nil {
		r.Fail(-999, err.Error())
	}
	r.Success(data)
	return r
}
func NewResults() *Results {
	return &Results{}
}
func (r *Results) NoLogin(code BizCode, msg string) *Results {
	r.Code = code
	r.Message = msg
	return r
}
func (r *Results) Fail(code BizCode, msg string) {
	r.Code = code
	r.Message = msg
}
